package com.sali.asyncollection.service;

import com.sali.asyncollection.service.impl.strategy.method1.AsynMethod01;
import com.sali.asyncollection.service.impl.strategy.method2.AsynMethod02;
import com.sali.asyncollection.service.impl.strategy.method3.AsynMethod03;
import com.sali.asyncollection.service.impl.strategy.method4.AsynMethod04;
import com.sali.asyncollection.service.impl.strategy.method5.AsynMethod05;
import com.sali.asyncollection.service.impl.strategy.method7.AsynMethod07;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutionException;

/**
 * @description:
 * @author: sali
 * @date: 2024/7/25 上午7:42.
 * @version: 1.0
 */
@Service
@Slf4j
public class OrderService {

    @Autowired
    private AsynMethod01 asynMethod01;

    @Autowired
    private AsynMethod02 asynMethod02;

    @Autowired
    private AsynMethod03 asynMethod03;

    @Autowired
    private AsynMethod04 asynMethod04;

    @Autowired
    private AsynMethod05 asynMethod05;

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private AsynMethod07 asynMethod07;


    public boolean makeOrder(Long userId, Long productId) throws ExecutionException, InterruptedException {
        log.info("用户：{}，下单产品ID是：{},并扣减库存", userId, productId);

//        asynMethod01.execute(userId);

//        asynMethod02.execute(userId);

//        asynMethod03.execute(userId);

//        asynMethod04.execute(userId);

        // @asyn实现异步
//        asynMethod05.sendMessage(userId);
//        asynMethod05.socreGet(userId);

        // 解耦 @asyn，依然得不到返回值
//        applicationContext.publishEvent(new OrderEvent(userId));

        // 通过 guava sdk 可以获取到异步执行异常结果
        asynMethod07.execute(userId);

        return true;
    }

}
